const { ethers } = require("hardhat");

async function main() {
  console.log("开始部署合约...");

  // 获取部署账户
  const [deployer] = await ethers.getSigners();
  console.log("部署账户:", deployer.address);
  console.log("账户余额:", (await deployer.getBalance()).toString());

  // 1. 部署 TraceBook 合约
  console.log("\n1. 部署 TraceBook 合约...");
  const TraceBook = await ethers.getContractFactory("TraceBook");
  const traceBook = await TraceBook.deploy();
  await traceBook.deployed();
  console.log("TraceBook 合约地址:", traceBook.address);

  // 2. 部署 BookIPFSManager 合约
  console.log("\n2. 部署 BookIPFSManager 合约...");
  const BookIPFSManager = await ethers.getContractFactory("BookIPFSManager");
  const ipfsManager = await BookIPFSManager.deploy();
  await ipfsManager.deployed();
  console.log("BookIPFSManager 合约地址:", ipfsManager.address);

  // 3. 部署 BOOK1155 合约
  console.log("\n3. 部署 BOOK1155 合约...");
  const BOOK1155 = await ethers.getContractFactory("BOOK1155");
  const book1155 = await BOOK1155.deploy(traceBook.address);
  await book1155.deployed();
  console.log("BOOK1155 合约地址:", book1155.address);

  // 4. 设置 IPFS 管理器地址
  console.log("\n4. 设置 IPFS 管理器地址...");
  await book1155.setIPFSManagerContract(ipfsManager.address);
  console.log("IPFS 管理器地址已设置");

  // 5. 设置 TraceBook 合约的授权
  console.log("\n5. 设置 TraceBook 合约授权...");
  await traceBook.setApprovalForAll(book1155.address, true);
  console.log("TraceBook 授权已设置");

  // 6. 部署 TokenMarketplace 合约
  console.log("\n6. 部署 TokenMarketplace 合约...");
  const TokenMarketplace = await ethers.getContractFactory("TokenMarketplace");
  const marketplace = await TokenMarketplace.deploy(traceBook.address);
  await marketplace.deployed();
  console.log("TokenMarketplace 合约地址:", marketplace.address);

  // 7. 设置 TokenMarketplace 的授权
  console.log("\n7. 设置 TokenMarketplace 授权...");
  await traceBook.setApprovalForAll(marketplace.address, true);
  console.log("TokenMarketplace 授权已设置");

  // 8. 部署 TokenCodeVerify 合约
  console.log("\n8. 部署 TokenCodeVerify 合约...");
  const TokenCodeVerify = await ethers.getContractFactory("TokenCodeVerify");
  const verifyContract = await TokenCodeVerify.deploy();
  await verifyContract.deployed();
  console.log("TokenCodeVerify 合约地址:", verifyContract.address);

  console.log("\n=== 部署完成 ===");
  console.log("合约地址汇总:");
  console.log("TraceBook:", traceBook.address);
  console.log("BookIPFSManager:", ipfsManager.address);
  console.log("BOOK1155:", book1155.address);
  console.log("TokenMarketplace:", marketplace.address);
  console.log("TokenCodeVerify:", verifyContract.address);

  // 保存合约地址到文件
  const addresses = {
    TraceBook: traceBook.address,
    BookIPFSManager: ipfsManager.address,
    BOOK1155: book1155.address,
    TokenMarketplace: marketplace.address,
    TokenCodeVerify: verifyContract.address,
    deployer: deployer.address
  };

  const fs = require('fs');
  fs.writeFileSync('deployed-addresses.json', JSON.stringify(addresses, null, 2));
  console.log("\n合约地址已保存到 deployed-addresses.json");
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  }); 